package com.tedu.egarticle2.controller;

import com.tedu.egarticle2.base.response.JsonResult;
import com.tedu.egarticle2.base.response.StatusCode;
import com.tedu.egarticle2.mapper.UserMapper;
import com.tedu.egarticle2.pojo.dto.UserArticleParam;
import com.tedu.egarticle2.pojo.dto.UserLoginParam;
import com.tedu.egarticle2.pojo.entity.Article;
import com.tedu.egarticle2.pojo.vo.ArticleVO;
import com.tedu.egarticle2.pojo.vo.UserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1/user")
@Validated
@Tag(name ="文章模块")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @PostMapping("/login")
    @Operation(summary = "登录")
    public JsonResult login(@RequestBody UserLoginParam userLoginParam, HttpSession session){
        UserVO userVO = userMapper.select(userLoginParam);
        session.setAttribute("user",userVO);
        return JsonResult.success(userVO);
    }
    @PostMapping("/article")
    @Operation(summary = "上传文章")
    public JsonResult article (@RequestBody @Validated UserArticleParam userArticleParam, HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        Article article = new Article();
        BeanUtils.copyProperties(userArticleParam,article);
        article.setUserId(userVO.getId());
        int num = userMapper.insert(article);
        return JsonResult.success();
    }
    @GetMapping("/selectByArticleId")
    @Operation(summary = "查看文章")
    public  JsonResult selectByArticle(@Range(min = 1,max = 100,message = "文章ID要求在1-100之间") Long id){
        ArticleVO articleVO = userMapper.selectByArticleId(id);
        return JsonResult.success(articleVO);
    }
}
